<template>
  <div style="margin: 10px 30px ;">
    <!-- form -->
    <el-form :model="form" inline style="max-width: 600px">
      <el-form-item label="SKU">
        <el-input v-model="form.sku" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="onSubmit">搜索</el-button>
      </el-form-item>
    </el-form>
    <el-button type="primary" @click="openDialog">新增</el-button>
    <!-- table -->
    <div style="margin-top: 20px;">
      <el-table :data="tableData" border style="width: 800px" max-height="660">
        <el-table-column label="SKU" width="500px">
          <template #default="scope">{{ scope.row.sku }}</template>
        </el-table-column>
        <el-table-column label="操作" width="300px">
          <template #default="scope">
            <el-button link type="primary" size="small" @click="modificationSKU(scope.row)">
              修改
            </el-button>
            <el-button link type="primary" size="small" @click="deleteSKU(scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <!-- dialog -->
    <el-dialog v-model="dialogVisible" width="500">
      <span>
        <el-form :model="dialogForm" inline style="max-width: 600px">
          <el-form-item label="SKU">
            <el-input v-model="dialogForm.sku" />
          </el-form-item>
        </el-form>
      </span>
      <template #footer>
        <div class="dialog-footer">
          <el-button @click="dialogVisible = false">取消</el-button>
          <el-button type="primary" @click="CreateSKU">
            确定
          </el-button>
        </div>
      </template>
    </el-dialog>
    <!-- page -->
    <div class="page">
      <el-pagination v-model:current-page="currentPage4" v-model:page-size="pageSize4" :page-sizes="[10, 50, 100]"
        layout="total, sizes, prev, pager, next, jumper" :total="total" @size-change="handleSizeChange"
        @current-change="handleCurrentChange" />
    </div>
  </div>

</template>

<script setup>
import { ref, onBeforeMount } from 'vue'
import { ElMessage } from 'element-plus'
import { passSkuOrderAdd, passSkuOrderList, passSkuOrderupdate, passSkuOrderdelete } from '@/api/index'
const form = ref({
  sku: '',
  pageSize: 10,
  pageNum: 1
})
const dialogForm = ref({
  sku: ''
})
const tableData = ref([])
onBeforeMount(() => {
  getList()
})
const dialogVisible = ref(false)
const openDialog = () => {
  setOrCreate.value = '1'
  dialogVisible.value = true
}
const getList = () => {
  passSkuOrderList(form.value).then(res => {
    const { data: { data } } = res
    console.log('data')
    console.log(data)
    tableData.value = data.list
    total.value = data.total
  })
}
const onSubmit = () => {
  form.value.pageNum = 1
  getList()
}
// 分页
const currentPage4 = ref(1)
const pageSize4 = ref(10)
const total = ref(0)
const handleSizeChange = (val) => {
  console.log(`${val} items per page`)
  form.value.pageSize = val
  getList()
}
const handleCurrentChange = (val) => {
  console.log(`current page: ${val}`)
  form.value.pageNum = val
  getList()
}
const CreateSKU = () => {
  if (setOrCreate.value === '1') {
    passSkuOrderAdd(dialogForm.value).then(res => {
      ElMessage.success('操作成功')
      getList()
      dialogVisible.value = false
    })
  } else if (setOrCreate.value === '2') {
    passSkuOrderupdate(dialogForm.value).then(res => {
      ElMessage.success('操作成功')
      getList()
      dialogVisible.value = false
    })
  }
  dialogForm.value = {
    sku: ''
  }
}
const setOrCreate = ref('1')
const modificationSKU = (row) => {
  setOrCreate.value = '2'
  dialogVisible.value = true
  dialogForm.value.sku = row.sku
  dialogForm.value.id = row.id
}
const deleteSKU = (row) => {
  passSkuOrderdelete({ id: row.id }).then(res => {
    ElMessage.success('操作成功')
    getList()
  })
}
</script>

<style lang='less' scoped></style>
